{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy聚合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.59531301e-01, 5.04371132e-01, 4.40175338e-01, 6.10315963e-01,\n",
       "       3.42011417e-01, 5.50556453e-01, 8.36533157e-01, 5.25203977e-02,\n",
       "       5.17756647e-01, 7.12484167e-01, 8.31740906e-01, 4.61429724e-01,\n",
       "       1.74851142e-01, 9.52908451e-01, 7.45215652e-03, 7.31007712e-01,\n",
       "       7.14071658e-02, 2.27789598e-01, 3.90570230e-01, 4.58187936e-01,\n",
       "       2.79507881e-01, 5.34291891e-01, 5.92111171e-01, 3.07576398e-01,\n",
       "       8.66395763e-01, 2.61808539e-02, 8.91012568e-01, 2.71711358e-01,\n",
       "       3.98502741e-01, 7.97803089e-01, 3.95209045e-01, 3.44213753e-04,\n",
       "       4.34034425e-01, 2.06356844e-01, 4.25457386e-01, 5.06038921e-02,\n",
       "       1.67275384e-02, 5.45495179e-01, 9.13734044e-01, 4.41422184e-01,\n",
       "       7.31366210e-02, 6.91951143e-01, 1.85725137e-01, 2.99935882e-01,\n",
       "       6.60876106e-01, 1.75528120e-01, 1.73525959e-01, 2.30367647e-01,\n",
       "       6.38498617e-01, 7.44484515e-01, 7.75356783e-01, 6.64477501e-01,\n",
       "       7.79964753e-01, 2.44337309e-01, 4.19688043e-02, 6.94908369e-01,\n",
       "       5.07657035e-01, 1.16303912e-01, 8.64434287e-01, 9.29334413e-01,\n",
       "       7.71859626e-01, 7.31552991e-01, 1.90971736e-01, 6.78486294e-01,\n",
       "       2.29039202e-01, 9.41781813e-01, 9.66738180e-01, 9.84805883e-01,\n",
       "       1.66797529e-01, 5.83843270e-01, 8.39025140e-01, 3.42196400e-01,\n",
       "       5.68117051e-01, 1.69187016e-01, 7.71122176e-01, 7.95758740e-02,\n",
       "       6.20570505e-01, 7.67646787e-01, 1.42730702e-01, 6.63340342e-01,\n",
       "       4.16557636e-03, 8.42358006e-01, 7.10004243e-02, 8.69219835e-01,\n",
       "       3.06134886e-01, 6.01116599e-01, 9.25209847e-01, 5.94756118e-01,\n",
       "       2.84303946e-01, 2.16010934e-01, 2.14573405e-01, 9.25519594e-01,\n",
       "       6.28219560e-01, 3.79314204e-01, 6.79103479e-01, 9.25288873e-01,\n",
       "       3.86093530e-01, 3.40551029e-02, 4.46972209e-01, 8.71731990e-01])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.random.random(100)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48.735389008300494"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48.73538900830047"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.random.rand(10**7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.84 s ± 323 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit sum(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17.3 ms ± 484 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit np.sum(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0003442137525345723"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9848058833957826"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48.73538900830047"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 矩阵聚合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.arange(16).reshape(4, -1)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(X) #矩阵所有数字相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[24 28 32 36]\n",
      "[ 6 22 38 54]\n"
     ]
    }
   ],
   "source": [
    "print(np.sum(X,axis = 0))\n",
    "print(np.sum(X,axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.prod(X) # 矩阵所有数字相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   0  585 1680 3465]\n",
      "[    0   840  7920 32760]\n"
     ]
    }
   ],
   "source": [
    "print(np.prod(X,axis = 0))\n",
    "print(np.prod(X,axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X)  # 求平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6. 7. 8. 9.]\n",
      "[ 1.5  5.5  9.5 13.5]\n"
     ]
    }
   ],
   "source": [
    "print(np.mean(X,axis = 0))\n",
    "print(np.mean(X,axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(X)  # 求中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6. 7. 8. 9.]\n",
      "[ 1.5  5.5  9.5 13.5]\n"
     ]
    }
   ],
   "source": [
    "print(np.median(X,axis = 0))\n",
    "print(np.median(X,axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.percentile(X,q=50) #百分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.0\n",
      "25 3.75\n",
      "50 7.5\n",
      "75 11.25\n",
      "100 15.0\n"
     ]
    }
   ],
   "source": [
    "for i in [0,25,50,75,100]:\n",
    "    print(i, np.percentile(X,i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6. 7. 8. 9.]\n",
      "[ 1.5  5.5  9.5 13.5]\n"
     ]
    }
   ],
   "source": [
    "print(np.percentile(X,q=50,axis = 0))\n",
    "print(np.percentile(X,q=50,axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21.25"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21.25"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((X - np.mean(X))**2)/np.size(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.6097722286464435"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(X) ** 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.normal(0,1,size=100000)  #构造正态分布一组数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.0016306788995623781"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9948384684238971"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(x)  # 计算标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
